package com.sinosoft.dsp.platform.action;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.event.SelectEvent;

import com.sinosoft.dsp.platform.components.portal.input.ISinoSearchFormDataViewBean;
import com.sinosoft.dsp.platform.components.portal.input.ISinoSearchFormViewBean;
import com.sinosoft.dsp.platform.components.portal.iteration.ISinoDataTableViewBean;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.platform.manager.ApplicationController;
import com.sinosoft.dsp.system.utils.StringUtil;

/**
 * 表格通用行为
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-6-15 上午02:22:07
 * 
 */
@ManagedBean(name = "dataTableAction")
@RequestScoped
public class DataTableAction {

	private static final Log log = Log.getLog(DataTableAction.class.getName());

	@ManagedProperty(value = "#{applicationController}")
	private ApplicationController applicationController;

	public void onRowSelect(SelectEvent event) throws SinoPlatformException {
		try {
			DataTable component = (DataTable) event.getSource();
			String id = component.getId();

			String[] ids = id.split("_");

			if (ids.length > 2 && StringUtil.isInteger(ids[ids.length - 2])
					&& "searchform".equals(ids[ids.length - 1])) {
				String dataIndexStr = ids[ids.length - 2];

				int dataIndex = Integer.valueOf(dataIndexStr);
				String viewBeanId = id.substring(0, id.length()
						- dataIndexStr.length() - 12);
//				ISinoSearchFormViewBean searchForm = (ISinoSearchFormViewBean) applicationController
//						.getViewBeanById(viewBeanId);
//				ISinoSearchFormDataViewBean data = searchForm.getSearchs()[dataIndex]
//						.getDatas();
//				data.onRowSelect(event);
			} else {
//				ISinoDataTableViewBean<?> dataViewBean = (ISinoDataTableViewBean<?>) applicationController
//						.getViewBeanById(id);
//				dataViewBean.onRowSelect(event);
			}

//		} catch (SinoPlatformException e) {
//			throw e;
		} catch (Exception e) {
			log.error("查询表单选择行出现错误。");
			throw new SinoPlatformException("查询表单选择行出现错误。");
		}
	}

	public ApplicationController getApplicationController() {
		return applicationController;
	}

	public void setApplicationController(
			ApplicationController applicationController) {
		this.applicationController = applicationController;
	}

}
